#include "data_global.h"
#include "leddriver.h"

void release_pthread_resource(int signo);

extern int fd_zigbee;

extern struct env_info_client_addr sm_all_env_info;

// 互斥锁
extern pthread_mutex_t mutex_client_request,
                mutex_refresh,
                mutex_sqlite,
                mutex_transfer,
                mutex_sms,
                mutex_zigbee,
                mutex_buzzer,
                mutex_led;

// 条件锁
extern pthread_cond_t  cond_client_request,
                cond_refresh,
                cond_sqlite,
                cond_transfer,
                cond_sms,
                cond_buzzer,
                cond_led;

extern int msgid;
extern int shmid;
extern int semid;

pthread_t id_client_request,
          id_refresh,
          id_sqlite,
          id_transfer,
          id_sms,
          id_buzzer,
          id_led;

int main(int argc, char const *argv[])
{
    /* code */
    pthread_mutex_init(&mutex_client_request, NULL);
    pthread_mutex_init(&mutex_refresh, NULL);
    pthread_mutex_init(&mutex_sqlite, NULL);
    pthread_mutex_init(&mutex_transfer, NULL);
    pthread_mutex_init(&mutex_sms, NULL);
    pthread_mutex_init(&mutex_zigbee, NULL);
    pthread_mutex_init(&mutex_buzzer, NULL);
    pthread_mutex_init(&mutex_led, NULL);

    signal(SIGINT, release_pthread_resource);

    pthread_mutex_lock(&mutex_transfer);
	bzero (&sm_all_env_info, sizeof (sm_all_env_info));
    pthread_mutex_unlock(&mutex_transfer);

    pthread_mutex_lock(&mutex_zigbee);
    if((fd_zigbee = open_port(ZIGBEE_DEV, 0)) == -1)
    {
        perror("zigbee open");
        // exit(1);
    }
    if (fd_zigbee != -1 && set_com_config(fd_zigbee, 115200, 8, 'N', 1) < 0)
    {
        perror("set_com_config");
        //exit(-1);
    }
    pthread_mutex_unlock(&mutex_zigbee);

    pthread_cond_init(&cond_client_request, NULL);
    pthread_cond_init(&cond_refresh, NULL);
    pthread_cond_init(&cond_sqlite, NULL);
    pthread_cond_init(&cond_transfer, NULL);
    pthread_cond_init(&cond_sms, NULL);
    pthread_cond_init(&cond_buzzer, NULL);
    pthread_cond_init(&cond_led, NULL);

    pthread_create(&id_client_request, NULL, pthread_client_request, NULL);
    pthread_create(&id_refresh, NULL, pthread_refresh, NULL);
    pthread_create(&id_sqlite, NULL, pthread_sqlite, NULL);
    pthread_create(&id_transfer, NULL, pthread_transfer, NULL);
    pthread_create(&id_sms, NULL, pthread_sms, NULL);
    pthread_create(&id_buzzer, NULL, pthread_buzzer, NULL);
    pthread_create(&id_led, NULL, pthread_led, NULL);

    pthread_join(id_client_request, NULL);  printf("pthread1\n");
    pthread_join(id_refresh, NULL);         printf("pthread2\n");
    pthread_join(id_sqlite, NULL);          printf("pthread3\n");
    pthread_join(id_transfer, NULL);        printf("pthread4\n");
    pthread_join(id_sms, NULL);             printf("pthread5\n");
    pthread_join(id_buzzer, NULL);          printf("pthread6\n");
    pthread_join(id_led, NULL);             printf("pthread7\n");

    return 0;
}

void release_pthread_resource(int signo)
{
    pthread_mutex_destroy(&mutex_client_request);
    pthread_mutex_destroy(&mutex_refresh);
    pthread_mutex_destroy(&mutex_sqlite);
    pthread_mutex_destroy(&mutex_transfer);
    pthread_mutex_destroy(&mutex_sms);
    pthread_mutex_destroy(&mutex_zigbee);
    pthread_mutex_destroy(&mutex_buzzer);
    pthread_mutex_destroy(&mutex_led);

    pthread_cond_destroy(&cond_client_request);
    pthread_cond_destroy(&cond_refresh);
    pthread_cond_destroy(&cond_sqlite);
    pthread_cond_destroy(&cond_transfer);
    pthread_cond_destroy(&cond_sms);
    pthread_cond_destroy(&cond_buzzer);
    pthread_cond_destroy(&cond_led);

    pthread_detach(id_client_request);
    pthread_detach(id_refresh);
    pthread_detach(id_sqlite);
    pthread_detach(id_transfer);
    pthread_detach(id_sms);
    pthread_detach(id_buzzer);
    pthread_detach(id_led);

    printf("all pthread is detached\n");

    msgctl(msgid, IPC_RMID, NULL);
    shmctl(shmid, IPC_RMID, NULL);
    semctl(semid, 1, IPC_RMID, NULL);

    if(fd_zigbee != -1) close(fd_zigbee);

    exit(0);
}

